package com.example.webviewdemo;

import com.example.utils.LogUtil;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

	WebView mWebView;
	
	//第二次改动
	
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mWebView = (WebView) findViewById(R.id.webview);

		mWebView.loadUrl("https://www.baidu.com/");

		//设置WebView的WebViewClient，里面可以处理一些事件
		mWebView.setWebViewClient(new MyWebViewClient());

		// 设置WebView支持获取手势焦点
		mWebView.requestFocusFromTouch();
		
		mWebView.setWebChromeClient(new WebChromeClient());

		WebSettings mWebSettings = mWebView.getSettings();

		// 设置支持JS
		mWebSettings.setJavaScriptEnabled(true);

		// 将图片调整到适合WebView的大小
		mWebSettings.setUseWideViewPort(true);

		// 支持缩放
		mWebSettings.setSupportZoom(true);

		// 关闭WebView中的缓存
		mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		LogUtil.i("onKeyDown");

		/**
		 * 当点击回退键时，不会销毁当前Activity，会返回到上一级的页面
		 */

		if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;

		}
		return super.onKeyDown(keyCode, event);
	}

	class MyWebViewClient extends WebViewClient {
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			LogUtil.i("shouldOverrideUrlLoading");
			// 表示当前的连接点击任然在当前的WebView中跳转，不会使用默认的浏览器
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
		}
		
		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			// TODO Auto-generated method stub
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
		
		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
			// TODO Auto-generated method stub
			super.onReceivedSslError(view, handler, error);
		}
		
		@Override
		public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
			// TODO Auto-generated method stub
			super.onReceivedHttpAuthRequest(view, handler, host, realm);
		}

	}

}
